package com.travel.order.service.impl;

import com.travel.common.to.SkuHasStockVo;
import com.travel.common.utils.R;
import com.travel.order.feign.StockFeignService;
import com.travel.order.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TicketServiceImpl implements TicketService {

    @Autowired
    StockFeignService stockFeignService;

    /**
     * 立即预订方法
     * @param spuId
     * @param selectedAttrs
     * @return
     */
    @Override
    public Boolean buyTicket(Integer spuId, String selectedAttrs) {
        //1. 判断是否有库存
        boolean b = skuHasStock(spuId, selectedAttrs);
        if(!b){ // 无库存
            return false;
        }

        return true;
    }

    /**
     * 判断该票是否有库存
     * @return
     */
    private boolean skuHasStock(Integer spuId,String selectedAttrs) {
        // 远程调用库存服务
        // 如果有库存返回true，否则返回false
        SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
        skuHasStockVo.setSpuId(spuId);
        skuHasStockVo.setSelectedAttrs(selectedAttrs);

        R r = stockFeignService.skuHasStock(skuHasStockVo);
        if(r.getCode() == 0){
            System.out.println("是否有库存"+r.get("stock"));
            return (boolean) r.get("stock");
        }else{
            System.out.println("远程调用库存服务失败");
            return false;
        }

    }
}
